#!/bin/bash

CALAMARRRADIOHOST=`getent hosts www.calamarrradio.org | gawk '{ print $1 }'`
CALAMARRRADIO="http://www.calamarrradio.org:8000/stream"

for i in host mpv gst-discoverer-1.0
do
    if ! [ -x "$(command -v $i)" ]; then
	echo "Error: $i is not installed. Please install it to run this program" >&2
	exit 1
    else
	echo "$i is installed"
    fi
done


echo "Checking availability of CalamarRradio"
echo "CalamarRradio has IP : $CALAMARRRADIOHOST"

if [ "`ping -c 1 $CALAMARRRADIOHOST &> /dev/null && echo success || echo fail`" = success ]
then
    echo "Calamarrradio in online, start playing"
else
    echo "Calamarrradio in offline, try later !"
    exit 1
fi

mpv --really-quiet $CALAMARRRADIO &

for pc in $(seq 1 1000000); do
    echo " "
    echo "Bienvenido a CalamarRradio !"
    echo "=========================="
    echo " "
    echo "Now playing : "
    echo " "
    # get the current artist and playing title
    gst-discoverer-1.0 $CALAMARRRADIO | egrep 'artista|título'
    echo " "
    # wait for 3 seconds
    sleep 3
    # clean the window
    tput reset
done
